package cn.fl.ass.controller.utils;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.text.NumberFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Font;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.Phrase;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;

import cn.cmm.base.framework.utils.BaseLogUtils;
import cn.cmm.base.framework.utils.DateUtils;
import cn.fl.ass.vo.AssTrailCarHvVO;

/**
 * Function: 导出PDF
 * Created by kfc on 2017/4/12.
 */
public class ExportAssUtils {

	private static final Logger logger = LoggerFactory.getLogger(ExportAssUtils.class);

    /**
     * 导出车辆交接单
     *
     * @param map
     * @return
     */
    public ByteArrayOutputStream exportAssCarHvPdf(Map<String, Object> map) {

        ByteArrayOutputStream baos = null;
        // 设置纸张大小
        Document document = new Document(PageSize.A4, 20, 20, 30, 30);
        NumberFormat nf = NumberFormat.getCurrencyInstance(Locale.CHINA);

        baos = new ByteArrayOutputStream();
        try {
            PdfWriter.getInstance(document, baos);

            // 设置中文
            BaseFont bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
            // 9号字体加粗
            Font FontBold = new Font(bfChinese, 9, Font.BOLD);
            // 9号 字体正常
            Font FontDefault = new Font(bfChinese, 9, Font.NORMAL);
            Font FontChinese = new Font(bfChinese, 12, Font.BOLD);
            Font FontDefault1 = new Font(bfChinese, 8, Font.BOLD);
            // 打开 document
            document.open();
            
            AssTrailCarHvVO vo1 = (AssTrailCarHvVO) map.get("vo1");
            AssTrailCarHvVO vo2 = (AssTrailCarHvVO) map.get("vo2");

            // 设置列宽
            float[] widthsPPC5 = {100f};
            PdfPTable tPdfPTable = new PdfPTable(widthsPPC5);
            tPdfPTable.setWidthPercentage(100f);
            tPdfPTable.addCell(makeCellWithNoBorder("  ", PdfPCell.ALIGN_CENTER, FontDefault1));
            tPdfPTable.addCell(makeCellWithNoBorder("狮桥资本车辆交接验收单", PdfPCell.ALIGN_CENTER, FontChinese));

            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy 年 MM 月 dd 日");
            String datetime = simpleDateFormat.format(new Date());
            tPdfPTable.addCell(makeCellWithNoBorder(datetime, PdfPCell.ALIGN_CENTER, FontDefault));
            document.add(tPdfPTable);
            float[] widthsPPC = {1f, 0.5f};
            tPdfPTable = new PdfPTable(widthsPPC);
            tPdfPTable.setWidthPercentage(100f);
            float[] widthsPPC1 = {0.1f, 0.2f, 0.1f, 0.2f};//六列
            tPdfPTable = new PdfPTable(widthsPPC1);
            tPdfPTable.setWidthPercentage(100f);
            tPdfPTable.addCell(makeCell("承租人:", PdfPCell.ALIGN_CENTER, FontBold));
            tPdfPTable.addCell(makeCell(null != vo1.getCstNm() ? vo1.getCstNm() : "", PdfPCell.ALIGN_CENTER, FontBold));
  
                
            tPdfPTable.addCell(makeCell("支付表号：", PdfPCell.ALIGN_CENTER, FontBold));
            tPdfPTable.addCell(makeCell(null != vo1.getPaySchNo() ? vo1.getPaySchNo() : "", PdfPCell.ALIGN_CENTER, FontBold));

            tPdfPTable.addCell(makeCell("车架号：", PdfPCell.ALIGN_CENTER, FontBold));
            tPdfPTable.addCell(makeCell(null != vo1.getPrdVin() ? vo1.getPrdVin() : "", PdfPCell.ALIGN_CENTER, FontBold));

            tPdfPTable.addCell(makeCell("车辆品牌：", PdfPCell.ALIGN_CENTER, FontBold));
            tPdfPTable.addCell(makeCell(null != vo1.getBrandNm() ? vo1.getBrandNm() : "", PdfPCell.ALIGN_CENTER, FontBold));

            tPdfPTable.addCell(makeCell("车辆类型：", PdfPCell.ALIGN_CENTER, FontBold));
            tPdfPTable.addCell(makeCell(null != vo1.getPrdType() ? vo1.getPrdType() : "", PdfPCell.ALIGN_CENTER, FontBold));

            tPdfPTable.addCell(makeCell("车身颜色：", PdfPCell.ALIGN_CENTER, FontBold));
            tPdfPTable.addCell(makeCell(null != vo2.getCarCl() ? vo2.getCarCl() : "", PdfPCell.ALIGN_CENTER, FontBold));

            tPdfPTable.addCell(makeCell("车长(米)：", PdfPCell.ALIGN_CENTER, FontBold));
            tPdfPTable.addCell(makeCell(null != vo2.getCarLen() ? vo2.getCarLen() : "", PdfPCell.ALIGN_CENTER, FontBold));

            tPdfPTable.addCell(makeCell("表征里程：", PdfPCell.ALIGN_CENTER, FontBold));
            tPdfPTable.addCell(makeCell(null != vo2.getTokenMileage() ? vo2.getTokenMileage() : "", PdfPCell.ALIGN_CENTER, FontBold));

            tPdfPTable.addCell(makeCell("车牌号：", PdfPCell.ALIGN_CENTER, FontBold));
            tPdfPTable.addCell(makeCell(null != vo2.getPlateNumber() ? vo2.getPlateNumber() : "", PdfPCell.ALIGN_CENTER, FontBold));
            
            tPdfPTable.addCell(makeCell("车辆所属省公司：", PdfPCell.ALIGN_CENTER, FontBold));
            tPdfPTable.addCell(makeCell(null != vo1.getProvincialCompany()?vo1.getProvincialCompany():"", PdfPCell.ALIGN_CENTER, FontBold));
            
            tPdfPTable.addCell(makeCell("保管城市：", PdfPCell.ALIGN_CENTER, FontBold));
            String provinceCd = null != vo1.getProvinceCd()?vo1.getProvinceCd():"";
            String cityCd = null != vo1.getCityCd()?vo1.getCityCd():"";
            tPdfPTable.addCell(makeCell(provinceCd+"--"+cityCd, PdfPCell.ALIGN_CENTER, FontBold));
            
            tPdfPTable.addCell(makeCell("是否正常行驶：", PdfPCell.ALIGN_CENTER, FontBold));
            tPdfPTable.addCell(makeCell(null != vo2.getIsRun() ? (vo2.getIsRun().toString().equals("1") ? "是" : "否"): "", PdfPCell.ALIGN_CENTER, FontBold));
            
            tPdfPTable.addCell(makeCell("行驶证：", PdfPCell.ALIGN_CENTER, FontBold));
            tPdfPTable.addCell(makeCell(null != vo2.getIsLicense() ? (vo2.getIsLicense().toString().equals("1") ? "有" : "无") : "", PdfPCell.ALIGN_CENTER, FontBold));

            tPdfPTable.addCell(makeCell("营运证：", PdfPCell.ALIGN_CENTER, FontBold));
            tPdfPTable.addCell(makeCell(null !=  vo2.getIsCertificate() ? (vo2.getIsCertificate().toString().equals("1") ? "有" : "无") : "", PdfPCell.ALIGN_CENTER, FontBold));

            tPdfPTable.addCell(makeCell("钥匙：", PdfPCell.ALIGN_CENTER, FontBold));
            tPdfPTable.addCell(makeCell(null !=  vo2.getIsKey() ? (vo2.getIsKey().toString().equals("1") ? "有" : "无") : "", PdfPCell.ALIGN_CENTER, FontBold));

            tPdfPTable.addCell(makeCell("质保手册：", PdfPCell.ALIGN_CENTER, FontBold));
            tPdfPTable.addCell(makeCell(null != vo2.getIsQuality() ? (vo2.getIsQuality().toString().equals("1") ? "有" : "无") : "", PdfPCell.ALIGN_CENTER, FontBold));

            tPdfPTable.addCell(makeCell("交强险：", PdfPCell.ALIGN_CENTER, FontBold));
            tPdfPTable.addCell(makeCell(null !=  vo2.getIsIns() ? (vo2.getIsIns().toString().equals("1") ? ("有(至"+ DateUtils.formatDateToString(vo2.getHandoverDt(), "yyyy-MM-dd"))+")": "无") : "", PdfPCell.ALIGN_CENTER, FontBold));
            
            tPdfPTable.addCell(makeCell("其他：", PdfPCell.ALIGN_CENTER, FontBold));
            tPdfPTable.addCell(makeCell(null != vo2.getOtherRem() ? vo2.getOtherRem(): "", PdfPCell.ALIGN_CENTER, FontBold));
            
            tPdfPTable.addCell(makeCell("轮胎：", PdfPCell.ALIGN_CENTER, FontBold));
            PdfPCell cell =  makeCell("应有"+(null != vo2.getTyreShouldQty() ? vo2.getTyreShouldQty() : "0")+"条，正常"+(null != vo2.getTyreNormalQty() ? vo2.getTyreNormalQty() : "0")+"条，损坏"+(null != vo2.getTyreDamageQty() ? vo2.getTyreDamageQty() : "0")+"条，缺失"+(null != vo2.getTyreDefectQty() ? vo2.getTyreDefectQty() : "0")+"条，备胎"+(null != vo2.getTyreSpareQty() ? vo2.getTyreSpareQty() : "0")+"条", PdfPCell.ALIGN_CENTER, FontBold);
            cell.setColspan(3);
            tPdfPTable.addCell(cell);
            
            tPdfPTable.addCell(makeCell("GPS编号：", PdfPCell.ALIGN_CENTER, FontBold));
            tPdfPTable.addCell(makeCell(null != vo2.getGpsCardNo() ?  vo2.getGpsCardNo() :"", PdfPCell.ALIGN_CENTER, FontBold));
            
            tPdfPTable.addCell(makeCell("车身重大损坏：", PdfPCell.ALIGN_CENTER, FontBold));
            tPdfPTable.addCell(makeCell(null != vo2.getIsDamage() ? (vo2.getIsDamage().toString().equals("1") ? "有" : "无") : "", PdfPCell.ALIGN_CENTER, FontBold));
            
            tPdfPTable.addCell(makeCell("停车场名称：", PdfPCell.ALIGN_CENTER, FontBold));
            tPdfPTable.addCell(makeCell(null != vo1.getParkNm()? vo1.getParkNm():"", PdfPCell.ALIGN_CENTER, FontBold));
            
            tPdfPTable.addCell(makeCell("停车场地址：", PdfPCell.ALIGN_CENTER, FontBold));
            tPdfPTable.addCell(makeCell(null != vo1.getParkAddress()? vo1.getParkAddress():"", PdfPCell.ALIGN_CENTER, FontBold));
            
            tPdfPTable.addCell(makeCell("交车人签字：", PdfPCell.ALIGN_CENTER, FontBold));
            tPdfPTable.addCell(makeCell(null != vo2.getHandoverNm() ? vo2.getHandoverNm() : "", PdfPCell.ALIGN_CENTER, FontBold));
            
            tPdfPTable.addCell(makeCell("接车人签字：", PdfPCell.ALIGN_CENTER, FontBold));
            tPdfPTable.addCell(makeCell(null != vo2.getReceiverNm() ? vo2.getReceiverNm() : "", PdfPCell.ALIGN_CENTER, FontBold));
            
            tPdfPTable.addCell(makeCell("交接日期：", PdfPCell.ALIGN_CENTER, FontBold));
            tPdfPTable.addCell(makeCell(null != vo2.getHandoverDt() ? DateUtils.formatDateToString(vo2.getHandoverDt(), "yyyy-MM-dd") : "", PdfPCell.ALIGN_CENTER, FontBold));
            
            tPdfPTable.addCell(makeCell("接车日期：", PdfPCell.ALIGN_CENTER, FontBold));
            tPdfPTable.addCell(makeCell(null != vo2.getReceiverDt() ? DateUtils.formatDateToString(vo2.getReceiverDt(), "yyyy-MM-dd") : "", PdfPCell.ALIGN_CENTER, FontBold));
            
            tPdfPTable.addCell(makeCell("备注：", PdfPCell.ALIGN_CENTER, FontBold));
            tPdfPTable.addCell(makeCell(null != vo2.getHandoverRem() ? vo2.getHandoverRem() : "", PdfPCell.ALIGN_CENTER, FontBold));
            
            tPdfPTable.addCell(makeCell("", PdfPCell.ALIGN_CENTER, FontBold));
            tPdfPTable.addCell(makeCell("", PdfPCell.ALIGN_CENTER, FontBold));
            document.add(tPdfPTable);

            
            // 加空行
            document.add(new Paragraph("\n", FontDefault));
            
            
            // 设置列宽
            float[] widthsPPC6 = {100f};
            tPdfPTable = new PdfPTable(widthsPPC6);
            tPdfPTable.setWidthPercentage(100f);
            tPdfPTable.addCell(makeCellWithNoBorder("  ", PdfPCell.ALIGN_CENTER, FontDefault1));
            tPdfPTable.addCell(makeCellWithNoBorder("狮桥资本车辆交接验收单-完善信息", PdfPCell.ALIGN_CENTER, FontChinese));
            document.add(tPdfPTable);
            // 加空行
            document.add(new Paragraph("\n", FontDefault));
            
            float[] widthsPPC2 = {0.1f, 0.2f, 0.1f, 0.2f};//六列
            tPdfPTable = new PdfPTable(widthsPPC2);
            tPdfPTable.setWidthPercentage(100f);
            tPdfPTable.setHeaderRows(1);
            
            tPdfPTable.addCell(makeCell("违章:", PdfPCell.ALIGN_CENTER, FontBold));
            tPdfPTable.addCell(makeCell("3分："+(null != vo2.getIllegalThree() ? vo2.getIllegalThree() : "")+"    6分："+(null != vo2.getIllegalSix() ? vo2.getIllegalSix() : "")+"    12分："+(null != vo2.getIllegalThree() ? vo2.getIllegalTwelve() : "") + "    其它："+(null != vo2.getIllegalOther() ? vo2.getIllegalOther() : "") , PdfPCell.ALIGN_CENTER, FontBold));
            
            tPdfPTable.addCell(makeCell("是否过户:", PdfPCell.ALIGN_CENTER, FontBold));
            tPdfPTable.addCell(makeCell(null != vo2.getIsTransfer() ? (vo2.getIsTransfer().toString().equals("1") ? "是" : "否") : "", PdfPCell.ALIGN_CENTER, FontBold));
            
            tPdfPTable.addCell(makeCell("有无重大事故:", PdfPCell.ALIGN_CENTER, FontBold));
            tPdfPTable.addCell(makeCell(null != vo2.getIsAccident() ? (vo2.getIsAccident().toString().equals("1") ? "有" : "无") : "", PdfPCell.ALIGN_CENTER, FontBold));
            
           
            tPdfPTable.addCell(makeCell("排放标准:", PdfPCell.ALIGN_CENTER, FontBold));
            tPdfPTable.addCell(makeCell(null != vo2.getEmiStd() ? vo2.getEmiStd() : "", PdfPCell.ALIGN_CENTER, FontBold));
            
            tPdfPTable.addCell(makeCell("交强险:", PdfPCell.ALIGN_CENTER, FontBold));
            tPdfPTable.addCell(makeCell(null !=  vo2.getIsIns() ? (vo2.getIsIns().toString().equals("1") ? ("有(至"+ DateUtils.formatDateToString(vo2.getInsDt(), "yyyy-MM-dd"))+")": "无") : "", PdfPCell.ALIGN_CENTER, FontBold));
            
            tPdfPTable.addCell(makeCell("商业险:", PdfPCell.ALIGN_CENTER, FontBold));
            tPdfPTable.addCell(makeCell(null !=  vo2.getIsCmIns() ? (vo2.getIsCmIns().toString().equals("1") ? ("有(至"+ DateUtils.formatDateToString(vo2.getCmInsDt(), "yyyy-MM-dd"))+")": "无") : "", PdfPCell.ALIGN_CENTER, FontBold));
            
            
            tPdfPTable.addCell(makeCell("发动机品牌:", PdfPCell.ALIGN_CENTER, FontBold));
            tPdfPTable.addCell(makeCell(null != vo2.getEngBrand() ? vo2.getEngBrand() : "", PdfPCell.ALIGN_CENTER, FontBold));
            
            tPdfPTable.addCell(makeCell("发动机型号:", PdfPCell.ALIGN_CENTER, FontBold));
            tPdfPTable.addCell(makeCell(null != vo2.getEngMdl() ? vo2.getEngMdl() : "", PdfPCell.ALIGN_CENTER, FontBold));
            
            tPdfPTable.addCell(makeCell("发动机马力:", PdfPCell.ALIGN_CENTER, FontBold));
            tPdfPTable.addCell(makeCell(null != vo2.getHp() ? vo2.getHp() : "", PdfPCell.ALIGN_CENTER, FontBold));
            
            tPdfPTable.addCell(makeCell("桥:", PdfPCell.ALIGN_CENTER, FontBold));
            tPdfPTable.addCell(makeCell(null != vo2.getBridge() ? vo2.getBridge() : "", PdfPCell.ALIGN_CENTER, FontBold));
            
            tPdfPTable.addCell(makeCell("出厂时间:", PdfPCell.ALIGN_CENTER, FontBold));
            tPdfPTable.addCell(makeCell(null != vo2.getPlayingDt() ? DateUtils.formatDateToString(vo2.getPlayingDt(), "yyyy-MM-dd"): "", PdfPCell.ALIGN_CENTER, FontBold));
            
            tPdfPTable.addCell(makeCell("上牌时间:", PdfPCell.ALIGN_CENTER, FontBold));
            tPdfPTable.addCell(makeCell(null != vo2.getLicDt() ? DateUtils.formatDateToString(vo2.getLicDt(), "yyyy-MM-dd"): "", PdfPCell.ALIGN_CENTER, FontBold));

            tPdfPTable.addCell(makeCell("车辆年审时间:", PdfPCell.ALIGN_CENTER, FontBold));
            tPdfPTable.addCell(makeCell(null != vo2.getCarAnnualDt() ? DateUtils.formatDateToString(vo2.getCarAnnualDt(), "yyyy-MM-dd") : "", PdfPCell.ALIGN_CENTER, FontBold));
            
            tPdfPTable.addCell(makeCell("营运证年审时间:", PdfPCell.ALIGN_CENTER, FontBold));
            tPdfPTable.addCell(makeCell(null != vo2.getCertificateAnnualDt() ? DateUtils.formatDateToString(vo2.getCertificateAnnualDt(), "yyyy-MM-dd") : "", PdfPCell.ALIGN_CENTER, FontBold));

            tPdfPTable.addCell(makeCell("上装配置:", PdfPCell.ALIGN_CENTER, FontBold));
            tPdfPTable.addCell(makeCell(null != vo2.getBoxDesc() ? vo2.getBoxDesc() : "", PdfPCell.ALIGN_CENTER, FontBold));

            tPdfPTable.addCell(makeCell("", PdfPCell.ALIGN_CENTER, FontBold));
            tPdfPTable.addCell(makeCell("", PdfPCell.ALIGN_CENTER, FontBold));

            document.add(tPdfPTable);


        } catch (DocumentException e) {
			BaseLogUtils.error(logger, "-", "异常", e);
        } catch (IOException e) {
			BaseLogUtils.error(logger, "-", "异常", e);
        } finally {
            document.close();
        }
        return baos;
    }

    /**
     * 创建一般 单元格
     */
    private PdfPCell makeCell(String ocontentntent, int align, Font FontDefault) {
        Phrase objPhase = new Phrase(ocontentntent, FontDefault);
        PdfPCell objCell = new PdfPCell(objPhase);
        objCell.setHorizontalAlignment(align);

        return objCell;
    }

    /**
     * 创建 无边框 单元格
     */
    private PdfPCell makeCellWithNoBorder(String content, int align, Font FontDefault) {
        Phrase objPhase = new Phrase(content, FontDefault);
        PdfPCell objCell = new PdfPCell(objPhase);
        objCell.setHorizontalAlignment(align);
        objCell.setBorder(0);
        return objCell;
    }
}
